//
//  ViewController.m
//  离线下载
//
//  Created by user on 16/4/18.
//  Copyright © 2016年 user. All rights reserved.
//
// 所需要下载的文件的URL
#define YCFileURL @"http://120.25.226.186:32812/resources/videos/minion_01.mp4"
// 文件名（沙盒中的文件名）
#define YCFilename YCFileURL.md5String
//文件路径(MD5 加密url 产生唯一性)
#define YCFilePath [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:YCFilename]

#define YCDownLoadLength [[[NSFileManager defaultManager] attributesOfItemAtPath:YCFilePath error:nil][NSFileSize] integerValue ]

// 存储文件总长度的文件路径（caches）
#define YCTotalLengthFullpath [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"totalLength.plist"]

#import "ViewController.h"
#import "NSString+Hash.h"
#import "YCFileDownLoadManager.h"
@interface ViewController ()

//@property(nonatomic,strong)NSURLSession *session;
//@property(nonatomic,strong)NSURLSessionDataTask *task;
//@property(nonatomic,strong)NSOutputStream *strem;
//@property(nonatomic,assign)NSInteger   totalLength;
//@property(nonatomic,assign)NSInteger   currentLength;

@property(nonatomic,strong)YCFileDownLoadManager* fileManger;
@end

@implementation ViewController
- (YCFileDownLoadManager *)fileManger
{
    if (!_fileManger) {
        _fileManger = [YCFileDownLoadManager sharedInstance];
    }
    return _fileManger;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.fileManger downLoadWithUrl:YCFileURL didComplete:^(CGFloat progress, NSString *fileURL) {
        NSLog(@"----%f,----%@",progress,fileURL);
        
    } comError:^(NSError *error) {
        
    }];
    
}
- (IBAction)startdownload:(id)sender {
    [self.fileManger resumeDownLoad];
  
    
}
- (IBAction)paruedownload:(id)sender {
    [self.fileManger suspendDownLoad];
}


//#pragma mark -- NSURLSessionDataDelegate
///*首先进入*/
//- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSHTTPURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
//{
//    [self.strem open];
//    self.totalLength= [response.allHeaderFields[@"Content-Length"] integerValue]+YCDownLoadLength;
//    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:YCTotalLengthFullpath];
//    if (dic==nil) {
//        dic = [NSMutableDictionary dictionary];
//    }
//    dic[YCFilename] = @(self.totalLength);
//    [dic writeToFile:YCTotalLengthFullpath atomically:YES];
//    completionHandler(NSURLSessionResponseAllow);
//}
///*接收到数据进入*/
//- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
//{
//    [self.strem write:data.bytes maxLength:data.length];
//    //获得当前下载文件长度
//
//    NSLog(@"%f,",1.0*YCDownLoadLength/self.totalLength);
//  
//}
///*完成*/
//- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
//{
//    [self.strem close];
//    self.strem = nil;
//}
//#pragma mark -- lazy
//-(NSOutputStream *)strem
//{
//    if (!_strem) {
// 
//
//        
//        _strem = [[NSOutputStream alloc]initToFileAtPath:YCFilePath append:YES];
//    }
//    
//    return _strem;
//}
//- (NSURLSession *)session
//{
//    
//    if (!_session) {
//         _session = [NSURLSession  sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
//    }
//    return _session;
//}
//- (NSURLSessionDataTask *)task
//{
//    if (!_task) {
//        NSInteger  totalL = [[NSDictionary dictionaryWithContentsOfFile:YCTotalLengthFullpath][YCFilename] integerValue ];
//        if (totalL && totalL == YCDownLoadLength)  return nil;
//        NSMutableURLRequest  *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:YCFileURL]];
//      
//        /*设置 请求从什么地方开始清求*/
//        NSString *range = [NSString stringWithFormat:@"bytes=%zd-",YCDownLoadLength];
//        [request setValue:range forHTTPHeaderField:@"Range"];
//        _task =   [self.session dataTaskWithRequest:request];
//    }
//    return _task;
//}
@end
